<?php
if (!defined('ADMIN_INIT'))
	exit ('Access Denied!');

// ---------- class server --------------------
class server {
	private $db = null;

	// ---------- function __construct --------------------
	public function __construct($db) {
		$this->db = $db;
	} // end function __construct

	// ---------- function __destruct --------------------
	public function __destruct() {
		unset ($this->db);
	} // end function __destruct

	public function provider_add($name, $server, $status, $type, $passwd) {
		if ($name != "" && $server != "" && $status != "" && $type != "") {
			$sql = "CALL kkyoo_query.sp_server_providerAdd('" . $name . "','" . $server . "','" . $status . "','" . $type . "','" . $passwd . "')";
			//echo $sql;exit;
			$result = $this->db->query($sql);
			//print_r($result);exit;
			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();

				next_result($this->db);
				return $row_arr['result'];
			}
		}

		return false;
	} // end function provider_add

	// ---------- function provider_add --------------------
	public function file_add($pid, $fid, $oldversion, $newversion, $path, $md5check, $mode) {
		if ($pid != "" && $fid != "" && $oldversion != "" && $newversion != "" && $path != "" && $md5check != "") {
			$sql = "CALL kkyoo_query.sp_server_fileAdd('" . $pid . "','" . $fid . "','" . $path . "','" . $md5check . "','" . $mode . "','" . $oldversion . "','" . $newversion . "')";
			//echo $sql;exit;
			$result = $this->db->query($sql);
			//print_r($result);exit;
			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();

				next_result($this->db);
				return $row_arr['result'];
			}
		}

		return false;
	} // end function provider_add

	// ---------- function provider_add --------------------
	public function file_list($pid, $fid, $path, $md5check, $oldversion, $newversion) {
		if ($pid > 0 && $fid > 0 && $path != "" && $md5check != "" && $oldversion != "" && $newversion != "") {
			$sql = "CALL kkyoo_query.sp_server_fileUp(" . $pid . "," . $fid . ",'" . $path . "','" . $md5check . "','" . $oldversion . "','" . $newversion . "')";
			//echo $sql;exit;
			$result = $this->db->query($sql);
			//print_r($result);exit;
			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();

				next_result($this->db);
				return $row_arr['result'];
			}
		}

		return false;
	} // end function provider_add

	// ---------- function provider_add --------------------
	public function fileupdate_add($pid, $fid, $addrurl) {
		if ($pid != "" && $fid != "" && $addrurl != "") {
			$sql = "CALL kkyoo_query.sp_server_fileupdateAdd('" . $pid . "','" . $fid . "','" . $addrurl . "')";
			//echo $sql;exit;
			$result = $this->db->query($sql);
			//print_r($result);exit;
			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();

				next_result($this->db);
				return $row_arr['result'];
			}
		}

		return false;
	} // end function provider_add

	// ---------- function host_show --------------------
	public function host_show($id) {
		if ($id > 0) {
			$sql = 'CALL kkyoo_admin.sp_core_hostShow(' . $id . ')';
			$result = $this->db->query($sql);

			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();

				next_result($this->db);
				return $row_arr;
			}
		}

		return false;
	} // end function host_show

	// ---------- function host_modify --------------------
	public function host_modify($id, $ip) {
		$len = strlen($ip);
		if ($len >= 8 && $len <= 15 && $id > 0) {
			$sql = 'CALL kkyoo_admin.sp_core_hostModify(' . $id . ',"' . $ip . '")';
			$result = $this->db->query($sql);

			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();

				next_result($this->db);
				return $row_arr['result'];
			}
		}

		return false;
	} // end function host_modify

	// ---------- function host_del --------------------
	public function host_del($id) {
		if ($id > 0) {
			$sql = 'CALL kkyoo_admin.sp_core_hostDel(' . $id . ')'; //echo $sql;exit;
			$result = $this->db->query($sql);

			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();

				next_result($this->db);
				return $row_arr['result'];
			}
		}

		return false;
	} // end function host_del

	// ---------- function getTotal --------------------
	private function getTotal($sql) {
		if ($sql != '') {
			$result = $this->db->query($sql);
			if (is_object($result)) {
				$row_arr = $result->fetch_assoc();
				$total = $row_arr['total'];

				next_result($this->db);
			}
		}

		return $total;
	} // end function getTotal

	// ---------- function turnpage --------------------
	private function turnpage($total, $perpage = 10) {
		if ($total > 0) {
			require_once 'page.class.php';
			$page = new extpage(array (
				'total' => $total,
				'perpage' => $perpage
			));
			$page_arr['page'] = $page->show();
			$page_arr['limit'] = $page->limit();
			unset ($page);
		}

		return $page_arr;
	} // function turnpage end
}
?>